

L = 1000; % liters of new ethanol at the margin;

%% Unit conversion constants:

G2L     = 3.78541; % Liters/Gallon
lbs2kg  = 0.454; % kg/lbs
kmsq2ha = 100; % ha/kmsq

%% Yield scenario:
y = 87.1; % sugarcane yield - ton/ha - (Macedo et al., 2008)
y = y*1.22; % (Macedo et al., 2008) - Projected increase 20 years

%% Technical parameters:
et2gas  = 0.75; % gasolin/ethanol (energy equivalence)
tonC2tonCO2 = 44/12; % (2006 IPCC Guidelines Vol.4, Chapter 2)
et_y = 86.3; % Ethanol yield - L/ton - (Macedo et al., 2008)

avoided_emissions = 1979; % Avoided emissions from ethanol use - kg CO2 equiv/m3 (Macedo, 2006)
avoided_emissions = (avoided_emissions/1000)/1000; % Avoided emissions from ethanol use - ton CO2 equiv/L (Macedo, 2006)
avoided_emissionsL = avoided_emissions*L; % Avoided emissions from ethanol use per L liters

%% Table acreage decomposition paper:

TableDec = TableExpansionDecomposition(:,[1,2,6]);
TableDec.ShareOfExpan = TableDec.ShareOfExpan./sum(TableDec.ShareOfExpan); % Normalize expansion share

%% From the model:
share_land     = TableElas.AcreageSweight/(TableElas.AcreageSweight+TableElas.YieldSweight); % Share of expansion at the margin coming from acreage
%wild_expansion = sum(TableExpansionDecomposition.ShareOfExpan(TableExpansionDecomposition.LU==1|TableExpansionDecomposition.LU==2));
forest_expansion = sum(TableDec.ShareOfExpan(TableDec.LU==1));
savana_expansion = sum(TableDec.ShareOfExpan(TableDec.LU==2));
farm_expansion = sum(TableDec.ShareOfExpan(TableDec.LU==3|TableDec.LU==4));

%% Emissions from land use change (Article Table 4: "Marginal Expansion and carbon emissions"):
%tropical_moist_deciduous = 220*(1+0.24)*0.47; % tonnes C/ha (2006 IPCC Guidelines Vol.4, Chapter 4)
tropical_moist_deciduous = 187*(1+0.28)*0.47; % tonnes C/ha (1019 Refinements to 2006 IPCC Guidelines Vol.4, Chapter 4)
tropical_moist_deciduous = tropical_moist_deciduous*kmsq2ha*tonC2tonCO2; % tonnes CO2 equiv/sqkm
tropical_shrub = 71.5*(1+0.28)*0.47; % tonnes C/ha (1019 Refinements to 2006 IPCC Guidelines Vol.4, Chapter 4)
tropical_shrub = tropical_shrub*kmsq2ha*tonC2tonCO2; % tonnes CO2 equiv/sqkm
%tropical_rain = 300*(1+0.37)*0.47; % tonnes C/ha (2006 IPCC Guidelines Vol.4, Chapter 4)
%tropical_rain = tropical_rain*kmsq2ha*tonC2tonCO2; % tonnes CO2 equiv/sqkm
TableDec.CO2Loss = [tropical_moist_deciduous;tropical_shrub;0;0];
TableDec.LU = []
writetable(TableDec,[opt.SaveResultsAs, '_TableDec.csv'], 'WriteRowNames', true);

%% Carbon payback computation:
y_kmsq = y*kmsq2ha; % sugarcane yield - ton/kmsq;
yl_kmsq = y_kmsq*et_y; % ethanol yield - L/kmsq;
pred_expansion = (1/yl_kmsq)*L*share_land; % Predicted expansion (kmsq) in growing from L liters of ethanol
emissions_direct_luchange_forest = tropical_moist_deciduous*pred_expansion*forest_expansion; % Direct emissions from deforestation of forests CO2 from L liters at the margin
emissions_direct_luchange_savana = tropical_shrub*pred_expansion*savana_expansion; % Direct emissions from deforestation of savana CO2 from L liters at the margin
emissions_direct_luchange = emissions_direct_luchange_forest + emissions_direct_luchange_savana;
ILUC_Scenarios = [0, 0.66, 1]; % Indirect Land Use Change scenarios
%emissions_indirect_luchange = farm_expansion*ILUC_Scenarios (wild_expansion/(1-farm_expansion))*ILUC_Scenarios...
%    *tropical_moist_deciduous*pred_expansion; % Indirect emissions from deforestation CO2 from L liters at the margin
emissions_indirect_luchange =  pred_expansion*farm_expansion*ILUC_Scenarios*( (forest_expansion/(forest_expansion + savana_expansion))*tropical_moist_deciduous + ...
    (savana_expansion/(forest_expansion + savana_expansion))*tropical_shrub);

ILUC_Scenarios = [0, 0.66, 1] % Indirect Land Use Change scenarios;
total_emissions = emissions_direct_luchange+emissions_indirect_luchange
payback_years   = total_emissions/avoided_emissionsL % Carbon payback years